package com.jiaai.cloud.wx.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jiaai.cloud.base.redis.util.RedisUtil;
import com.jiaai.cloud.base.util.GeoHash;
import com.jiaai.cloud.base.util.LatLngDistance;
import com.jiaai.cloud.wx.entity.TWxgoods;
import com.jiaai.cloud.wx.entity.TWxshop;
import com.jiaai.cloud.wx.entity.TWxuser;
import com.jiaai.cloud.wx.mapper.TWxshopMapper;
import com.jiaai.cloud.wx.mapper.TWxuserMapper;
import com.jiaai.cloud.wx.service.ITWxshopService;
import com.jiaai.cloud.wx.service.ITWxuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-22
 */
@Service
public class TWxuserServiceImpl extends ServiceImpl<TWxuserMapper, TWxuser> implements ITWxuserService {

    @Autowired
    private TWxshopMapper tWxshopMapper;
    @Autowired
    private TWxuserMapper tWxuserMapper;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 得到用户附近的商店列表
     * @param longitude
     * @param latitude
     * @return
     */
    @Override
    public Object getNearShops(Double longitude, Double latitude, String shopname, Page page) {
        String myGeoHash = new GeoHash(latitude, longitude).getGeoHashBase32().toString();
        String shopList = redisUtil.hashGet(1,"firstPage",myGeoHash);
        //1小时内, 所有在myGeoHash内访问的user, 共用一个shopList

        if (shopList == null || shopList.trim().length() == 0){
            List<TWxshop> list = tWxshopMapper.selectFromGeoHash(
                    new GeoHash(latitude, longitude)
                            .getGeoHashBase32()
                            .substring(0, 3)
                    , shopname);

            //没有数据直接返回
            if (list == null || list.size() == 0) return null;

            for (TWxshop e : list){
                double lat = Double.parseDouble(e.getLat());
                double lng = Double.parseDouble(e.getLng());
                e.setDistance(LatLngDistance.getDistance(lat, lng, latitude, longitude));
            }

//            list.removeIf((o1) -> {
//                if (o1.getDistance() > 30) return true;
//                return false;
//            });
            list.sort((o1, o2) -> {
                if (o1.getDistance() > o2.getDistance()) return 1;
                else if (o1.getDistance() < o2.getDistance())  return -1;
                return 0;
            });
            redisUtil.hashSet(1, "geoHashKey", myGeoHash, JSON.toJSONString(list), (60 * 60 * 1));
            return list;
        }else {
            return JSON.parse(shopList);
        }
    }

    @Override
    public TWxuser selectUserByOpenID(String openID) {
        return tWxuserMapper.selectUserByOpenID(openID);
    }

    @Override
    public void insertBackUserid(TWxuser tWxuser) {
        tWxuserMapper.insertBackUserid(tWxuser);
    }

    @Override
    public List<TWxshop> selectUserShops(Integer userid) {
        List<TWxshop> list = tWxuserMapper.selectUserShops(userid);
        for (TWxshop e : list){
            List<TWxgoods> list1 = e.getGoods();
        }

        return list;
    }

    public static void main(String[] args) {
        List<TWxuser> list = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            TWxuser tWxuser = new TWxuser();
            tWxuser.setNickname("asdf"+i);
            tWxuser.setAvater("asdf"+i);
            tWxuser.setUserid(i);
            list.add(tWxuser);
        }

        String t = JSON.toJSONString(list);

        list = (List<TWxuser>) JSON.parse(t);
        System.out.println(list);

    }

}
